//
//  WSInputYzmView.m
//  FamilyLinkPro
//
//  Created by 江鹏 on 2023/9/6.
//  Copyright © 2023 SiterWell.Co.,Ltd. All rights reserved.
//

#import "WSInputYzmView.h"

#define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
#define selectColor RGBA(211, 233, 255, 1)
#define noSelectColor RGBA(242, 245, 248, 1)


@implementation WSInputYzmView


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubviews];
       
    }
    return self;
}


// MARK: 添加子控件
- (void)setupSubviews {
    UITapGestureRecognizer *viewTouchRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTouchClick)];
    [self addGestureRecognizer:viewTouchRecognizer];
    
    _sigleTextFieldW = (_width_ - 30 - 20*5)/6;
    
    UILabel *yzmLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    yzmLabel.text = @"输入验证码";
    [self addSubview:yzmLabel];
    yzmLabel.textColor = UIColorFromRGB_0x(0x000000);
    yzmLabel.font = fontBold(24);
    [yzmLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(20);
        make.top.mas_equalTo(self).offset(NaviHeight + 22);
    }];
    
    UILabel *yzmMetionLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [self addSubview:yzmMetionLabel];
    yzmMetionLabel.numberOfLines = 0;
    yzmMetionLabel.textColor = UIColorFromRGB_0x(0x666666);
    yzmMetionLabel.font = font(12);
    [yzmMetionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(yzmLabel);
        make.right.mas_equalTo(self).offset(-27);
        make.top.mas_equalTo(yzmLabel.mas_bottom).offset(13);
    }];
    yzmMetionLabel.text = [NSString stringWithFormat:@"验证码已发送至您的邮箱:%@",@"xxxxxxxxxxxxxxxxxxx"];
    
    [self textFieldWithYzmMethonLabel:yzmMetionLabel];
    
    
    UIButton *retrySendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [retrySendButton beginResumeCountdownFunctionCountDownTextColor:UIColorFromRGB_0x(0x999999) withRestoreTitleColor:UIColorFromRGB_0x(0x4C80F8)];
    [self addSubview:retrySendButton];
    retrySendButton.titleLabel.font = fontBold(14);
    [retrySendButton setTitleColor:UIColorFromRGB_0x(0x999999) forState:UIControlStateNormal];
    retrySendButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [retrySendButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(yzmMetionLabel);
        make.top.mas_equalTo(_view1.mas_bottom).offset(30);
        make.width.mas_equalTo(155);
        make.height.mas_equalTo(25);
    }];
    [retrySendButton addTarget:self action:@selector(retrySendButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
}


- (void)textFieldWithYzmMethonLabel:(UILabel *)yzmMetionLabel {
    _view1 = [self setupSuperView:self withBackColor:selectColor withYzmMethonLabel:yzmMetionLabel];
    _codeTextFiled1 = [self setupKeyInputTextField:_view1 withTag:1 withIsHidden:NO];
    _view1_mask = [self setupView_mask:_view1 isHidden:YES];
    
    
    _view2 = [self setupSuperView:_view1 withBackColor:noSelectColor withYzmMethonLabel:yzmMetionLabel];
    _codeTextFiled2 = [self setupKeyInputTextField:_view2 withTag:2 withIsHidden:YES];
    _view2_mask = [self setupView_mask:_view2 isHidden:NO];
    
    _view3 = [self setupSuperView:_view2 withBackColor:noSelectColor withYzmMethonLabel:yzmMetionLabel];
    _codeTextFiled3 = [self setupKeyInputTextField:_view3 withTag:3 withIsHidden:YES];
    _view3_mask = [self setupView_mask:_view3 isHidden:NO];
    
    _view4 = [self setupSuperView:_view3 withBackColor:noSelectColor withYzmMethonLabel:yzmMetionLabel];
    _codeTextFiled4 = [self setupKeyInputTextField:_view4 withTag:4 withIsHidden:YES];
    _view4_mask = [self setupView_mask:_view4 isHidden:NO];
    
    _view5 = [self setupSuperView:_view4 withBackColor:noSelectColor withYzmMethonLabel:yzmMetionLabel];
    _codeTextFiled5 = [self setupKeyInputTextField:_view5 withTag:5 withIsHidden:YES];
    _view5_mask = [self setupView_mask:_view5 isHidden:NO];
    
    _view6 = [self setupSuperView:_view5 withBackColor:noSelectColor withYzmMethonLabel:yzmMetionLabel];
    _codeTextFiled6 = [self setupKeyInputTextField:_view6 withTag:6 withIsHidden:YES];
    _view5_mask = [self setupView_mask:_view6 isHidden:NO];

   
}


- (UIView *)setupSuperView:(UIView *)potionView
             withBackColor:(UIColor *)backgroundColor
        withYzmMethonLabel:(UILabel *)yzmMetionLabel {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = backgroundColor;
    view.layer.cornerRadius = 8;
    [self addSubview:view];
    if (potionView == self) {
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(_sigleTextFieldW);
            make.top.mas_equalTo(yzmMetionLabel.mas_bottom).offset(30);
            make.left.mas_equalTo(potionView.mas_left).offset(15);
        }];
    }else {
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(_sigleTextFieldW);
            make.top.mas_equalTo(yzmMetionLabel.mas_bottom).offset(30);
            make.left.mas_equalTo(potionView.mas_right).offset(20);
        }];
    }
    
    return view;
}

- (KeyInputTextField *)setupKeyInputTextField:(UIView *)superView
                                      withTag:(NSInteger)tag
                                 withIsHidden:(BOOL)isHidden {
    KeyInputTextField *codeTextFiled = [[KeyInputTextField alloc] initWithFrame:CGRectZero];
    codeTextFiled.backgroundColor = ClearColor;
    codeTextFiled.alpha = 1.0;
    codeTextFiled.textAlignment = NSTextAlignmentCenter;
    codeTextFiled.keyboardType = UIKeyboardTypeNumberPad;
    codeTextFiled.tag = tag;
    codeTextFiled.hidden = isHidden;
    codeTextFiled.delegate = self;
    codeTextFiled.keyInputDelegate = self;
    [codeTextFiled addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
    [self addSubview:codeTextFiled];
    [codeTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top);
        make.left.mas_equalTo(superView.mas_left);
        make.bottom.mas_equalTo(superView.mas_bottom);
        make.right.mas_equalTo(superView.mas_right);
    }];
    return codeTextFiled;
}


- (UIView *)setupView_mask:(UIView *)superView
                  isHidden:(BOOL)isHidden {
    UIView *view_mask = [[UIView alloc] init];
    view_mask.backgroundColor = RGBA(0, 0, 0, 0);
    view_mask.layer.cornerRadius = 8;
    view_mask.hidden = isHidden;
    [self addSubview:view_mask];
    [view_mask mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top);
        make.left.mas_equalTo(superView.mas_left);
        make.bottom.mas_equalTo(superView.mas_bottom);
        make.right.mas_equalTo(superView.mas_right);
    }];
    return view_mask;
}

// MARK: action

- (void)retrySendButtonClick:(UIButton *)sender {
   [sender beginResumeCountdownFunctionCountDownTextColor:UIColorFromRGB_0x(0x999999) withRestoreTitleColor:UIColorFromRGB_0x(0x4C80F8)];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(requestYzmClick)]) {
        [self.delegate requestYzmClick];
    }
}


// MARK: KeyInputTextFieldDelegate

- (void)deleteBackward {
    int aa = self.currentIndex - 1;
    [self selectShow:aa];
    switch(self.currentIndex) {
        case 1:
            self.codeTextFiled1.text = @"";
        case 2:
            self.codeTextFiled2.text = @"";
        case 3:
            self.codeTextFiled3.text = @"";
        case 4:
            self.codeTextFiled4.text = @"";
        case 5:
            self.codeTextFiled5.text = @"";
        case 6:
            self.codeTextFiled6.text = @"";
    }
}

// MARK: UITextFieldDelegate

- (void)textFieldChanged:(UITextField *)textField {
    NSString * string2 = textField.text;
   // NSLog(@"输入的字：%@",string2);
    NSInteger tag = textField.tag;
    switch(tag){
        case 1:
            if([NSString isBlankString:string2]){
                
            }else{
                [self selectShow:2];
            }
            break;
        case 2:
            if([NSString isBlankString:string2]){
                [self selectShow:1];
            }else{
                [self selectShow:3];
            }
            break;
        case 3:
            if([NSString isBlankString:string2]){
                [self selectShow:2];
            }else{
                [self selectShow:4];
            }
            break;
        case 4:
            if([NSString isBlankString:string2]){
                [self selectShow:3];
            }else{
                [self selectShow:5];
            }
            break;
        case 5:
            if([NSString isBlankString:string2]){
                [self selectShow:4];
            }else{
                [self selectShow:6];
            }
            break;
        case 6:
            if([NSString isBlankString:string2]){
                [self selectShow:5];
            }else{
                
                NSString *str1 = self.codeTextFiled1.text;
                NSString *str2 = self.codeTextFiled2.text;
                NSString *str3 = self.codeTextFiled3.text;
                NSString *str4 = self.codeTextFiled4.text;
                NSString *str5 = self.codeTextFiled5.text;
                NSString *str6 = self.codeTextFiled6.text;
                
                [self validateVerifyCode:[NSString stringWithFormat:@"%@%@%@%@%@%@",str1,str2,str3,str4,str5,str6]];
            }
            break;
    }
    
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([NSString isBlankString:string]) {
        return YES;
    }
    // 网关限定不超过15个字符长度
    if (textField.text.length>=1)
        return NO; // return NO to not change text
    return YES;
}


// MARK: methon

#pragma mark -- 开始跟服务器校验验证码
-(void)validateVerifyCode:(NSString *)verficode {
    if (self.delegate && [self.delegate respondsToSelector:@selector(validateVerifyCode:)]) {
        [self.delegate validateVerifyCode:verficode];
    }
}


-(void)selectShow:(int)index{
    self.currentIndex = index;
    if(index==1){
        self.view1_mask.hidden = YES;
        self.view2_mask.hidden = NO;
        self.view3_mask.hidden = NO;
        self.view4_mask.hidden = NO;
        self.view5_mask.hidden = NO;
        self.view6_mask.hidden = NO;
        
        self.codeTextFiled1.hidden = NO;
        self.view1.backgroundColor = selectColor;
        self.codeTextFiled2.hidden = YES;
        self.view2.backgroundColor = noSelectColor;
        self.codeTextFiled3.hidden = YES;
        self.view3.backgroundColor = noSelectColor;
        self.codeTextFiled4.hidden = YES;
        self.view4.backgroundColor = noSelectColor;
        self.codeTextFiled5.hidden = YES;
        self.view5.backgroundColor = noSelectColor;
        self.codeTextFiled6.hidden = YES;
        self.view6.backgroundColor = noSelectColor;
        self.codeTextFiled2.text = @"";
        self.codeTextFiled3.text = @"";
        self.codeTextFiled4.text = @"";
        self.codeTextFiled5.text = @"";
        self.codeTextFiled6.text = @"";
        [self.codeTextFiled1 becomeFirstResponder];
    }else  if(index==2){
        self.view1_mask.hidden = NO;
        self.view2_mask.hidden = YES;
        self.view3_mask.hidden = NO;
        self.view4_mask.hidden = NO;
        self.view5_mask.hidden = NO;
        self.view6_mask.hidden = NO;
        
        self.codeTextFiled1.hidden = NO;
        self.view1.backgroundColor = noSelectColor;
        self.codeTextFiled2.hidden = NO;
        self.view2.backgroundColor = selectColor;
        self.codeTextFiled3.hidden = YES;
        self.view3.backgroundColor = noSelectColor;
        self.codeTextFiled4.hidden = YES;
        self.view4.backgroundColor = noSelectColor;
        self.codeTextFiled5.hidden = YES;
        self.view5.backgroundColor = noSelectColor;
        self.codeTextFiled6.hidden = YES;
        self.view6.backgroundColor = noSelectColor;
        self.codeTextFiled3.text = @"";
        self.codeTextFiled4.text = @"";
        self.codeTextFiled5.text = @"";
        self.codeTextFiled6.text = @"";
        [self.codeTextFiled2 becomeFirstResponder];
    }else  if(index==3){
        self.view1_mask.hidden = NO;
        self.view2_mask.hidden = NO;
        self.view3_mask.hidden = YES;
        self.view4_mask.hidden = NO;
        self.view5_mask.hidden = NO;
        self.view6_mask.hidden = NO;
        
        self.codeTextFiled1.hidden = NO;
        self.view1.backgroundColor = noSelectColor;
        self.codeTextFiled2.hidden = NO;
        self.view2.backgroundColor = noSelectColor;
        self.codeTextFiled3.hidden = NO;
        self.view3.backgroundColor = selectColor;
        self.codeTextFiled4.hidden = YES;
        self.view4.backgroundColor = noSelectColor;
        self.codeTextFiled5.hidden = YES;
        self.view5.backgroundColor = noSelectColor;
        self.codeTextFiled6.hidden = YES;
        self.view6.backgroundColor = noSelectColor;
        self.codeTextFiled4.text = @"";
        self.codeTextFiled5.text = @"";
        self.codeTextFiled6.text = @"";
        [self.codeTextFiled3 becomeFirstResponder];
    }else  if(index==4){
        self.view1_mask.hidden = NO;
        self.view2_mask.hidden = NO;
        self.view3_mask.hidden = NO;
        self.view4_mask.hidden = YES;
        self.view5_mask.hidden = NO;
        self.view6_mask.hidden = NO;
        self.codeTextFiled1.hidden = NO;
        self.view1.backgroundColor = noSelectColor;
        self.codeTextFiled2.hidden = NO;
        self.view2.backgroundColor = noSelectColor;
        self.codeTextFiled3.hidden = NO;
        self.view3.backgroundColor = noSelectColor;
        self.codeTextFiled4.hidden = NO;
        self.view4.backgroundColor = selectColor;
        self.codeTextFiled5.hidden = YES;
        self.view5.backgroundColor = noSelectColor;
        self.codeTextFiled6.hidden = YES;
        self.view6.backgroundColor = noSelectColor;
        self.codeTextFiled5.text = @"";
        self.codeTextFiled6.text = @"";
        [self.codeTextFiled4 becomeFirstResponder];
    }else  if(index==5){
        self.view1_mask.hidden = NO;
        self.view2_mask.hidden = NO;
        self.view3_mask.hidden = NO;
        self.view4_mask.hidden = NO;
        self.view5_mask.hidden = YES;
        self.view6_mask.hidden = NO;
        self.codeTextFiled1.hidden = NO;
        self.view1.backgroundColor = noSelectColor;
        self.codeTextFiled2.hidden = NO;
        self.view2.backgroundColor = noSelectColor;
        self.codeTextFiled3.hidden = NO;
        self.view3.backgroundColor = noSelectColor;
        self.codeTextFiled4.hidden = NO;
        self.view4.backgroundColor = noSelectColor;
        self.codeTextFiled5.hidden = NO;
        self.view5.backgroundColor = selectColor;
        self.codeTextFiled6.hidden = YES;
        self.view6.backgroundColor = noSelectColor;
        self.codeTextFiled6.text = @"";
        [self.codeTextFiled5 becomeFirstResponder];
    }else  if(index==6){
        self.view1_mask.hidden = NO;
        self.view2_mask.hidden = NO;
        self.view3_mask.hidden = NO;
        self.view4_mask.hidden = NO;
        self.view5_mask.hidden = NO;
        self.view6_mask.hidden = YES;
        self.codeTextFiled1.hidden = NO;
        self.view1.backgroundColor = noSelectColor;
        self.codeTextFiled2.hidden = NO;
        self.view2.backgroundColor = noSelectColor;
        self.codeTextFiled3.hidden = NO;
        self.view3.backgroundColor = noSelectColor;
        self.codeTextFiled4.hidden = NO;
        self.view4.backgroundColor = noSelectColor;
        self.codeTextFiled5.hidden = NO;
        self.view5.backgroundColor = noSelectColor;
        self.codeTextFiled6.hidden = NO;
        self.view6.backgroundColor = selectColor;
        [self.codeTextFiled6 becomeFirstResponder];
    }
}



// MARK: action

- (void)viewTouchClick {
    
}



@end
